package com.luo.chengrui.module.sa.service.commontree;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServerException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import com.luo.chengrui.module.sa.constant.SaConstants;
import com.luo.chengrui.module.sa.controller.admin.commontree.vo.CommonTreeCreateReqVO;
import com.luo.chengrui.module.sa.controller.admin.commontree.vo.CommonTreeExportReqVO;
import com.luo.chengrui.module.sa.controller.admin.commontree.vo.CommonTreePageReqVO;
import com.luo.chengrui.module.sa.controller.admin.commontree.vo.CommonTreeUpdateReqVO;
import com.luo.chengrui.module.sa.convert.commontree.CommonTreeConvert;
import com.luo.chengrui.module.sa.dal.dataobject.commontree.CommonTreeDO;
import com.luo.chengrui.module.sa.dal.mysql.commontree.CommonTreeMapper;
import com.luo.chengrui.module.sa.service.commontree.CommonTreeService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.luo.chengrui.module.sa.enums.ErrorCodeConstants.*;;

/**
 * 通用树 Service 实现类
 *
 * @author 诚睿
 */
@Service
@Validated
public class CommonTreeServiceImpl implements CommonTreeService {

    @Resource
    private CommonTreeMapper commonTreeMapper;

    @Override
    public String createCommonTree(CommonTreeCreateReqVO createReqVO) {

        String parentId = createReqVO.getParentId();

        if (StrUtil.isBlank(parentId)) {
            throw exception(COMMON_TREE_PARENT_IS_NOT_NULL);
        }

        if (COMMON_TREE_ROOT.equals(parentId)) {
            //添加一级分类时，验证类型是否已经存在；
            CommonTreeDO commonTreeDO = new CommonTreeDO();
            commonTreeDO.setParentId(parentId);
            commonTreeDO.setKindId(createReqVO.getKindId());
            List<CommonTreeDO> list = commonTreeMapper.selectList(commonTreeDO);
            if (list != null && list.size() > 0) {
                throw exception(COMMON_TREE_CODE_EXISTS);
            }
        }

        // 插入
        CommonTreeDO commonTree = CommonTreeConvert.INSTANCE.convert(createReqVO);
        commonTreeMapper.insert(commonTree);

        if (COMMON_TREE_ROOT.equals(parentId)) {
            commonTree.setFullId(SaConstants.FULL_PATH_SEPARATE + commonTree.getId());
            commonTree.setFullName(SaConstants.FULL_PATH_SEPARATE + commonTree.getName());
        } else {
            CommonTreeDO parent = commonTreeMapper.selectById(createReqVO.getParentId());
            commonTree.setFullId(parent.getFullId() + SaConstants.FULL_PATH_SEPARATE + commonTree.getId());
            commonTree.setFullName(parent.getFullName() + SaConstants.FULL_PATH_SEPARATE + commonTree.getName());
        }
        commonTreeMapper.updateById(commonTree);
        // 返回
        return commonTree.getId();
    }

    @Override
    public void updateCommonTree(CommonTreeUpdateReqVO updateReqVO) {
        // 校验存在
        validateCommonTreeExists(updateReqVO.getId());
        if (COMMON_TREE_ROOT.equals(updateReqVO.getParentId())) {
            updateReqVO.setFullName(SaConstants.FULL_PATH_SEPARATE + updateReqVO.getName());
            updateReqVO.setFullId(SaConstants.FULL_PATH_SEPARATE + updateReqVO.getId());
        } else {
            CommonTreeDO parent = commonTreeMapper.selectById(updateReqVO.getParentId());
            updateReqVO.setFullId(parent.getFullId() + SaConstants.FULL_PATH_SEPARATE + updateReqVO.getId());
            updateReqVO.setFullName(parent.getFullName() + SaConstants.FULL_PATH_SEPARATE + updateReqVO.getName());
        }
        // 更新
        CommonTreeDO updateObj = CommonTreeConvert.INSTANCE.convert(updateReqVO);
        commonTreeMapper.updateById(updateObj);
    }

    @Override
    public void deleteCommonTree(String id) {
        // 校验存在
        validateCommonTreeExists(id);
        // 删除
        commonTreeMapper.deleteById(id);
    }

    private void validateCommonTreeExists(String id) {
        if (commonTreeMapper.selectById(id) == null) {
            throw exception(COMMON_TREE_NOT_EXISTS);
        }
    }

    @Override
    public CommonTreeDO getCommonTree(String id) {
        return commonTreeMapper.selectById(id);
    }

    @Override
    public List<CommonTreeDO> getCommonTreeList(Collection<String> ids) {
        return commonTreeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CommonTreeDO> getCommonTreePage(CommonTreePageReqVO pageReqVO) {
        return commonTreeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CommonTreeDO> getCommonTreeList(CommonTreeExportReqVO exportReqVO) {

        return commonTreeMapper.selectList(exportReqVO);
    }

    @Override
    public List<CommonTreeDO> getChildrenBy(String id) {
        CommonTreeExportReqVO reqVO = new CommonTreeExportReqVO();
        List<CommonTreeDO> resultList = new ArrayList<>();
        reqVO.setParentId(id);
        if (StrUtil.isBlank(id)) {
            CommonTreeDO root = new CommonTreeDO();
            root.setName("根节点");
            root.setId(COMMON_TREE_ROOT);
            root.setCode(COMMON_TREE_ROOT);
            resultList.add(root);
            return (resultList);
        }
        return commonTreeMapper.selectList(reqVO);
    }

    @Override
    public List<CommonTreeDO> getCommonTreeAllList(CommonTreeExportReqVO exportReqVO) {
        return commonTreeMapper.getCommonTreeAllList(exportReqVO);
    }
}
